package com.bw.task;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.LogUtils;
import com.bw.task.adapter.MyTab;
import com.bw.task.adapter.MyViewpagerAdapter;
import com.bw.task.fragment.FiveFragment;
import com.bw.task.fragment.ForeFragment;
import com.bw.task.fragment.OneFragment;
import com.bw.task.fragment.ThreeFragment;
import com.bw.task.fragment.TowFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;


import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ViewPager vp;
    private CommonTabLayout commtab;
    long pretime =0;
    ArrayList<Fragment> fragments;
    ArrayList<CustomTabEntity> customTabEntities = new ArrayList<>();
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断按下的是否是返回按键
        if (keyCode==KeyEvent.KEYCODE_BACK){
            //上一次按下返回的时间
            long currentTimeMillis = System.currentTimeMillis();
            if (currentTimeMillis-pretime>2000){
                Toast.makeText(this, "再点一次退出", Toast.LENGTH_SHORT).show();
                pretime=System.currentTimeMillis();
                return true;
            }else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        requestPermissions(new String[]{
                 Manifest.permission.CAMERA
                ,Manifest.permission.WRITE_EXTERNAL_STORAGE
                ,Manifest.permission.READ_EXTERNAL_STORAGE
        }, 100);

        fragments = new ArrayList<>();
        fragments.add(new OneFragment());
        fragments.add(new TowFragment());
        fragments.add(new ThreeFragment());
        fragments.add(new ForeFragment());
        fragments.add(new FiveFragment());
        MyViewpagerAdapter myViewpagerAdapter = new MyViewpagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, fragments);
        vp.setAdapter(myViewpagerAdapter);
        customTabEntities.add(new MyTab("首页"));
       customTabEntities.add(new MyTab("发现"));
       customTabEntities.add(new MyTab(R.drawable.camera_fals,R.drawable.camera_true));
       customTabEntities.add(new MyTab("消息"));
       customTabEntities.add(new MyTab("我的"));
       commtab.setTabData(customTabEntities);

       commtab.setOnTabSelectListener(new OnTabSelectListener() {
           @Override
           public void onTabSelect(int position) {
                   vp.setCurrentItem(position);
           }

           @Override
           public void onTabReselect(int position) {

           }
       });
       vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
           @Override
           public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
           }

           @Override
           public void onPageSelected(int position) {
                    commtab.setCurrentTab(position);
           }

           @Override
           public void onPageScrollStateChanged(int state) {

           }
       });


    }

    private void initView() {
        vp = (ViewPager) findViewById(R.id.vp);
        commtab = (CommonTabLayout) findViewById(R.id.commtab);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.i("--------11111100");
        if (requestCode== PictureSelector.SELECT_REQUEST_CODE){
            if (data!=null){
                PictureBean parcelableExtra = data.getParcelableExtra(PictureSelector.PICTURE_RESULT);

//                LogUtils.i(parcelableExtra.getPath());
                Uri uri = parcelableExtra.getUri();
                String path = parcelableExtra.getPath();
                LogUtils.i("--------111111"+uri);
                Message obtain = Message.obtain();
                obtain.what=200;
                obtain.obj=path;

                OneFragment.handler.sendMessage(obtain);
//                viewById.setImageBitmap(BitmapFactory.decodeFile(parcelableExtra.getPath()));
            }
        }
    }
}